package com.uzai.console.service.app.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.app.base.AppBaseInfoDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.app.AppBaseService;
import com.uzai.console.vo.app.base.AppBaseInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class AppBaseServiceImpl implements AppBaseService {

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Object queryAppBaseInfo(Long merId) {

        AppBaseInfoVo appBaseInfoVo=new AppBaseInfoVo();

        //查询运营商APP基础设置信息
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.APP_BASE_CONFIG.getValue());

        if(merBaseConfig!=null){
            appBaseInfoVo= JSONObject.parseObject(merBaseConfig.getVal(),AppBaseInfoVo.class);
        }else {
            //没有 读取默认配置
             merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.CMS_BASE_CONFIG.getValue());

            if(merBaseConfig!=null){
                appBaseInfoVo= JSONObject.parseObject(merBaseConfig.getVal(),AppBaseInfoVo.class);
            }else {
                throw new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "未查询到cms基础配置");
            }
        }

        return appBaseInfoVo;
    }

    @Override
    public void updateAppBaseInfo(AppBaseInfoDto appBaseInfoDto) {


        AppBaseInfoVo appBaseInfoVo=new AppBaseInfoVo();
        BeanUtils.copyProperties(appBaseInfoDto,appBaseInfoVo);

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(appBaseInfoDto.getMerId(), MerConfigKeyEnum.APP_BASE_CONFIG.getValue());

        int nowTime = DateUtil.getNowTime();

        //修改
        if(merBaseConfig!=null){
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(appBaseInfoVo)));
            merBaseConfig.setUpdateTime(nowTime);
            merBaseConfigMapper.update(merBaseConfig);
        }
        //新增
        else {
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(appBaseInfoDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.APP_BASE_CONFIG.getValue());
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(appBaseInfoVo)));
            merBaseConfig.setCreateTime(nowTime);
            merBaseConfig.setUpdateTime(nowTime);
            merBaseConfigMapper.insert(merBaseConfig);
        }

        //保存到Redis当中
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.APP_BASE_CONFIG.getKey(), appBaseInfoDto.getMerId()), merBaseConfig.getVal());
    }


    @Override
    public Object copyCmsBaseInfo(Long merId) {
        AppBaseInfoVo appBaseInfoVo=new AppBaseInfoVo();
        //查询运营商CMS基础设置信息
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.CMS_BASE_CONFIG.getValue());

        if(merBaseConfig!=null){
            appBaseInfoVo= JSONObject.parseObject(merBaseConfig.getVal(),AppBaseInfoVo.class);
        }else {
            throw new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "未查询到cms基础配置");
        }
        return appBaseInfoVo;
    }
}
